#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>

typedef struct queue
{
    char *array; // 队列元素
    int front;   // 队头位置
    int rear;    // 队尾位置
    int size;    // 队列的大小
} Queue;

// 创建队列
Queue *create(unsigned int size)
{
    Queue *q = (Queue *)malloc(sizeof(Queue));
    q->front = q->rear = 0; // 将队头和队尾都都置为0
    q->size = size;         // 初始化队列的大小
    // 动态申请数组的地址，size 个 char 字节的大小内存
    q->array = (char *)malloc(q->size * sizeof(char));

    return q;
}

int queueEmpty(Queue *q)
{
    return q->front == q->rear;
}

int queueFull(Queue *q)
{
    // 如果队尾位置+1 取队列长度 等于队头的位置则说明 队满
    return q->front == (q->rear + 1) % q->size;
}

void enqueue(Queue *q, char x)
{
    if (queueFull(q))
    {
        printf("the queue is full, can't be enqueue element\n");
        return;
    }

    q->array[q->rear] = x;
    q->rear = (q->rear + 1) % q->size;
    printf("enqueue value>>%c, front=%d, rear=%d\n", x, q->front, q->rear);
}

char dequeue(Queue *q)
{
    if (queueEmpty(q))
    {
        printf("the queue is empty, can't be dequeue element\n");
        return CHAR_MIN;
    }

    char x = q->array[q->front];
    // 获取队尾的位置
    q->front = (q->front + 1) % q->size;
    printf("dequeue value>>%c, front=%d, rear=%d\n", x, q->front, q->rear);
    return x;
}

char geFrontElement(Queue *q)
{
    if (queueEmpty(q))
    {
        printf("the queue is empty, can't be dequeue element\n");
        return CHAR_MIN;
    }

    return q->array[q->front];
}

int main()
{
    // Queue *Q = create(11);
    // char *chs = "debgh";
    // printf("--------------start enqueue >>>%s\n", chs);
    // for (size_t i = 0; i < strlen(chs); i++)
    // {
    //     enqueue(Q, chs[i]);
    // }
    // printf("--------------dequeue >>>%c%c\n", chs[0], chs[1]);
    // for (size_t i = 0; i < 2; i++)
    // {
    //     dequeue(Q);
    // }

    // char *chs1 = "ijklm";
    // printf("--------------start enqueue >>>%s\n", chs1);
    // for (size_t i = 0; i < strlen(chs1); i++)
    // {
    //     enqueue(Q, chs1[i]);
    // }

    // printf("--------------dequeue >>>b\n");
    // dequeue(Q);
    
    // char *chs2 = "nopqr";
    // printf("--------------dequeue >>>%s\n", chs2);
    // for (size_t i = 0; i < strlen(chs2); i++)
    // {
    //     enqueue(Q, chs2[i]);
    // }

    // exit(0);
    Queue *myQueue = create(10);
    printf("---------------start queue operate-----------\n");
    char ch;
    while ((ch = getchar()) != '\n')
    {
        enqueue(myQueue, ch);
    }
    printf("---------------dequeue-----------\n");

    while (!queueEmpty(myQueue))
    {
        dequeue(myQueue);
    }
    

    return 0;
}